//
//  ST_BTIMEmojiMessageModel.swift
//  btvideo
//
//  Created by mac on 2020/7/15.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import NIMSDK
import SwiftyJSON


class ST_BTMessageAttachDecoder: NSObject,NIMCustomAttachment,NIMCustomAttachmentCoding {
    
    
    func decodeAttachment(_ content: String?) -> NIMCustomAttachment? {
        
        guard let str = content else { return nil }
        
        let json = JSON.init(parseJSON: str)
        
        
//        安卓使用 0
        switch json["msgType"].intValue {
            
            /// 新增女性只能发出邀请通话
        case 335:
            
            let model = ST_BTFemaleInviteChatMessageModel()
            
            model.inviteType = json["type"].stringValue == "2" ? 2 : 1
            
            return model
        
//        新增【通话记录】 类型消息
        case 270:
            
            let model = ST_Tian270AttachmentModel()
            
            if let t = ST_TIANRecordType.init(rawValue: json["type"].intValue){
                model.type = t
            }

            model.agoraType = json["agoraType"].intValue
            
            model.time = json["time"].floatValue
            
            
            
            
            return model
            
        case 231:
            
            let model = ST_BTIMGiftMessageModel()
            
            model.type = ST_IMGoodsType.init(rawValue: json["type"].intValue) ?? ST_IMGoodsType.gift
            
            model.goodsId = json["goodsId"].stringValue
            model.title = json["title"].stringValue
            model.subTitle = json["subTitle"].stringValue
            model.imageUrl = json["imageUrl"].stringValue
            
            return model
            
        case 233:
            
            let model = ST_BTIMEmojiMessageModel()

            model.imageUrl = json["imageUrl"].stringValue
            
            model.name = json["name"].stringValue
            
            return model
            
        case 255:
            
            let model = ST_Custom255AttachmentModel()

            model.type = json["type"].intValue
            model.redPacketId = json["redPacketId"].stringValue
            model.redPacketCoinNum = json["redPacketCoinNum"].intValue
            model.redPacketIncome = json["redPacketIncome"].floatValue
            model.redPacketTxt = json["redPacketTxt"].stringValue
            model.goodsId = json["goodsId"].stringValue
            model.title = json["title"].stringValue
            model.subTitle = json["subTitle"].stringValue
            model.imageUrl = json["imageUrl"].stringValue
            if let url = json["svgaUrl"].string,url.count > 2{
                model.svgaUrl = url
            }
            return model
        case 2649:
            
            let model = ST_Custom2649AttachmentModel()
            
            model.type = json["type"].intValue
            model.wechatText = json["wechatText"].stringValue
            
            return model
            
            
        case 264:
            
            let model = ST_Custom264AttachmentModel()
            
            model.text = json["text"].stringValue
            
            return model
            
        case 313:
            
            let model = ST_BTIMInviteMessageModel()
            
            model.roomId = json["roomId"].stringValue
            model.nickname = json["nickname"].stringValue
            model.avatar = json["avatar"].stringValue
            
            return model
        default:
            return nil
        }
    }
    
    
    func encode() -> String {
        return ""
    }
    
}



class ST_BTIMEmojiMessageModel:ST_BTMessageAttachDecoder{
    
    var imageUrl: String = ""
    var name: String = ""
    
    
    override func encode() -> String {
        
        let dict = ["imageUrl":self.imageUrl,"name":self.name,"msgType":233] as [String : Any]
        
        let json = JSON(dict)
        
        let res = json.rawString() ?? ""
        
        return res
        
    }
}

